package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.NewsListAdapter;
import com.yaozheng.vocationaltraining.adapter.TrainingBulletinAdapter;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.iview.ITrainingBulletinView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.NewsListService;
import com.yaozheng.vocationaltraining.service.impl.NewsListServiceImpl;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 培训公告
 * 
 * @author Administrator
 * 
 */
@EActivity(R.layout.activity_news_list)
public class TrainingBulletinActivity extends BaseActivity implements
		ITrainingBulletinView, SwipeRefreshLayout.OnRefreshListener,
		ILoadMoreView {
	@ViewById
	ListView listView;

	@Bean(NewsListServiceImpl.class)
	NewsListService newsListService;
	/**
	 * 下拉刷新
	 */
	@ViewById
	SwipeRefreshLayout swipeRefreshLayout;

	int lastId;
	@Extra
	int cat;

	TrainingBulletinAdapter trainingBulletinAdapter;
	/**
	 * 操作json
	 */
	JSONObject operatingJsonObject;

	/**
	 * 管理
	 */
	ListViewLoadMoreManage listViewLoadMoreManage;

	@AfterViews
	public void initView() {
		setHeadTitle("培训公告");

		newsListService.init(this);

//		swipeRefreshLayout.postDelayed(new Runnable() {
//
//			@Override
//			public void run() {
//				swipeRefreshLayout.setRefreshing(true);
//				onRefresh();
//
//			}
//		}, 500);
		onRefresh();
		swipeRefreshLayout.setOnRefreshListener(this);
		listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
				listView);
	}

	/**
	 * 加载数据
	 */
	public void loadData() {
		newsListService.newsList(cat, lastId);
	}

	@UiThread
	@Override
	public void responseSuccess(JSONObject jsonObject) {
		cancelLoadDataProgressDialog();
		JSONArray newsesDataList = TypeUtils.getJsonArray(jsonObject, "data",
				"newses");
		if (newsesDataList != null) {
			if (trainingBulletinAdapter == null) {
				trainingBulletinAdapter = new TrainingBulletinAdapter(this,
						newsesDataList, this);
				listView.setAdapter(trainingBulletinAdapter);

			} else {
				if (lastId == 0) {
					trainingBulletinAdapter.getDataList().clear();
				}
				trainingBulletinAdapter.getDataList().addAll(newsesDataList);
				trainingBulletinAdapter.notifyDataSetChanged();
			}
		}
	}

	@UiThread
	@Override
	public void responseError(String error) {
		cancelLoadDataProgressDialog();
		alertMessage(error);
	}

	@Override
	public void cancelLoadDataProgressDialog() {
		swipeRefreshLayout.setRefreshing(false);
		listViewLoadMoreManage.loadMoreComplete();
	}

	@Override
	public void onRefresh() {
		lastId = 0;
		loadData();
	}

	@Override
	public void signUp(int position) {
		if (operatingJsonObject != null) {
			return;
		}
		if (trainingBulletinAdapter != null) {
			if (position < 0)
				return;
			operatingJsonObject = TypeUtils.getJsonObject(
					trainingBulletinAdapter.getDataList(), position);
			if (operatingJsonObject == null)
				return;
			showOperatingProgressDialog("操作中");
			newsListService.signUp(
					TypeUtils.getJsonInteger(operatingJsonObject, "id"),
					TypeUtils.getJsonBoolean(operatingJsonObject, "applied"));
		}
	}

	@UiThread
	@Override
	public void signUpSuccess(JSONObject returnJsonObject) {
		cancelOperatingProgressDialog();
		boolean returnBool = TypeUtils.getJsonBoolean(
				TypeUtils.getJsonObject(returnJsonObject, "data"), "success",
				false);
		if (returnBool && operatingJsonObject != null) {
			try {
				boolean applied = TypeUtils.getJsonBoolean(operatingJsonObject,
						"applied");
				operatingJsonObject.put("applied", !applied);
				if (trainingBulletinAdapter != null) {
					trainingBulletinAdapter.notifyDataSetChanged();
				}
			} catch (Exception e) {
				ErrorUtils.outErrorLog(e);
			}
		}
		operatingJsonObject = null;

	}

	@UiThread
	@Override
	public void signUpError(String error) {
		cancelOperatingProgressDialog();
		operatingJsonObject = null;
		alertMessage(error);
	}

	@Override
	public void loadMoreData() {
		if (trainingBulletinAdapter != null) {
			int lastId = trainingBulletinAdapter.getLastId();
			if (lastId > 0) {
				this.lastId = lastId;
				loadData();
			}
		}
	}

	@Override
	public boolean isResponseResult() {
		return !isFinishing();
	}
}
